<?php
declare(strict_types=1);

namespace app\api\controller;

use app\api\validate\User;
use app\BaseController;
use app\common\lib\Show;
use think\exception\ValidateException;
use app\common\business\Sms as SmsBus;

class Sms extends BaseController
{
    /**
     * 发送短信
     * @return \think\response\Json
     */
    public function code()
    {
        $phoneNumber = input("param.phone_number", '', '');
        $data = [
            'phone_number' => $phoneNumber
        ];
        //验证
        try {
            validate(User::class)->scene('send_code')->check($data);
        } catch (ValidateException $e) {
            return Show::fail(config("status.error"), $e->getError());
        }
        if (!SmsBus::getInstance()->sendCode($phoneNumber,6,'ali')) {
            return Show::fail(config('status.error'), "发送验证码失败");
        }
        return Show::success(null, "发送验证码成功");
    }
}